import { login } from "@/http/user"
export let user = {
    namespaced: true,
    state: {
        token: localStorage.getItem("token"),
        get username() {
            return localStorage.getItem("username")
        },
        set username (v) {
            if(v){
                localStorage.setItem("username",v)
            }else{
                localStorage.removeItem("username")
            }
        },
    
    },
    actions: {
        async loginFn({ commit }, obj) {
            let res = await login(obj)
            if (res.meta.status == 200) {
                commit("SET_TOKEN", res.data.token)
                commit("SET_USERNAME", res.data.username)
                return Promise.resolve()
            } else {
                return Promise.reject()
            }
        }
    },
    mutations: {
        SET_TOKEN(state, token) {
            state.token = token;
            localStorage.setItem("token",token)
        },
        SET_USERNAME(state, username) {
            state.username = username
        },
        DEL_TOKEN(state){
            localStorage.removeItem("token")
        },
        DEL_USERNAME(state){
            state.username=""
        }
    }
}